Key-Based Authentication for SFTP
To get key-based SFTP authentication for a customer:
- Customer should provide a public key
- Customer may run
ssh-keygenfrom their own machine to generate a key underC:\Users\USERNAME\.ssh\[cipher].pub- this is a public key and is safe for them to share with us
- Customer may run
- Save the content from the .pub file somewhere on the relevant gHost SFTP server (e.g.
C:\Users\YourUser\Downloads) (this process will need to be done on both the WEB-1 & WEB-2 servers, if the customer is a PROD customer) - On SALES-GHOST, under
C:\Scripts & Utilities, is a PowerShell script namedSFTP_PublicKey_Adding - Open the script in PowerShell ISE on the SFTP server that the customer will be connecting to (in line with above, if the customer is in PROD, this will need to be done on each server)
- Update the 'domainUser', 'sshFolder' and 'publicKey' variables appropriately
- Run the script to create all necessary files & folders, and apply the correct permissions. You may need to run this twice, manually opening the newly-created '.ssh' folder in Windows Explorer between the first and second run.
- Customer should now be able to remotely log in i.e. if they run
sftp username@poc.ghost.sitethey should be auto-logged in, without needing a password- The 'domain' portion should not be needed for log in
OpenSSH is extremely particular, so if any of the files/folders have incorrect permissions, key-based authentication will not work
Testing
Once the above PowerShell script has been run, you can test remotely from a machine that has the OpenSSH client installed.
Follow the instructions to install the OpenSSH client. Then, open PowerShell on your machine and run ssh-keygen to create your own public key under C:\Users\USERNAME\.ssh\[cipher].pub.
Copy the text from inside the .pub file, and add it on a new line to the customer's 'authorized_keys' file (inside the .ssh folder under the customer's home directory, on the relevant SFTP server). Include whatever username is at the end of the key - this is a comment and is never really used.
Now from your own machine, in PowerShell, run sftp customersusername@relevantserver.ghost.site and you should be auto-logged in.
Note that you will still need to add the user to the relevant AD group, and create a relevant Match User rule in sshd.config on the appropriate server, to ensure that the user is properly contained to within their own folder. Instructions for this can be found under Receiving Customer Data.
Errors
If there are errors:
- Uncomment the 'log level' line in sshd_config and change the value from INFO to DEBUG3
- Restart the OpenSSH service
- Open Windows Event Viewer and navigate to
Applications and Services Logs > Open SSH. Enable the Debug logs. - Run the sftp command with the -vvv flag to get extra debug information:
sftp -vvv -i ~/.ssh/id_ed25519 gsa-CustomerName@poc.ghost.site- Note that in this command, the '-i ~/.ssh/id_ed25519' component is forcing the connection to use the private key located under your
C:\Users\USERNAME\.ssh\folder. This is the private key that corresponds to the same public key, which you copied into the customer's 'authorized_keys' folder on the SFTP server.
- Now move through the Event Viewer logs line by line to try and determine the underlying cause of the issue. For example,
sshd: debug3: Bad owner on C:/Users/customer1/.ssh/authorized_keysindicates the permissions are wrong, and the subsequent log message may say that the authorized_keys file has been skipped.
BACKUP/DO NOT USE, THIS IS A BRAIN DUMP:
- On our SFTP server, run a PowerShell window under the customer's domain account
- Run
ssh-keygen - This creates the necessary '.ssh' folder under the customer's account
- Create a new file under the customer's '.ssh' folder named 'authorized_keys' (no file extension)
- Copy the public key text that the customer created, into this 'authorized_keys' file